var fdata={};
var jsondata;
function init_libres_form(level, wwwroot, cid) {
    jsondata = YAHOO.lang.JSON.stringify(fdata);
	var url = wwwroot+'/blocks/moodlelibreserve/block_moodlelibreserve_forms.php?level='+level+'&data='+jsondata+'&cid='+cid;
	var AjaxObject = {
		handleSuccess:function (o) {
			document.getElementById('libres-form').innerHTML = o.responseText;
		},

		handleFailure:function (o) {
			alert(o.error+'blablabla');
		},

		startRequest:function () {
			YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
		}
	};

	var callback = {
		success:AjaxObject.handleSuccess,
		failure:AjaxObject.handleFailure,
		scope:AjaxObject
	};

	AjaxObject.startRequest();
}

function save_comment(level, wwwroot, comment, id) {
	var url = wwwroot+'/blocks/moodlelibreserve/block_moodlelibreserve_forms.php?level='+level+'&comment='+comment+'&id='+id;
	var AjaxObject = {
		handleSuccess:function (o) {
			document.getElementById('existing_notes'+id).innerHTML = o.responseText;
		},

		handleFailure:function (o) {
			alert(o.error+'blablabla');
		},

		startRequest:function () {
			YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
		}
	};

	var callback = {
		success:AjaxObject.handleSuccess,
		failure:AjaxObject.handleFailure,
		scope:AjaxObject
	};

	AjaxObject.startRequest();
}


function get_reserve(id, wwwroot){
	var url = wwwroot+'/blocks/moodlelibreserve/block_moodlelibreserve_forms.php?level=6&get_reserve='+id;
	
	var AjaxObject = {
		handleSuccess:function (o) {
			fdata = YAHOO.lang.JSON.parse(o.responseText);
			init_libres_form(1, wwwroot);
		},

		handleFailure:function (o) {
			alert(o.error+'boom');
		},

		startRequest:function () {
			YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
		}
	};

	var callback = {
		success:AjaxObject.handleSuccess,
		failure:AjaxObject.handleFailure,
		scope:AjaxObject
	};

	AjaxObject.startRequest();


}

function delete_request(id, wwwroot){
	var doDelete = confirm('Are you sure you want to delete this request?');
	if(doDelete){
		var url = wwwroot+'/blocks/moodlelibreserve/block_moodlelibreserve_forms.php?level=9&delete_request='+id;
		
		var AjaxObject = {
			handleSuccess:function (o) {
				reserveBox = document.getElementById('reserve_box_'+id);
				reserveBox.style.display = 'none';
			},

			handleFailure:function (o) {
				alert(o.error+'boom');
			},

			startRequest:function () {
				YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
			}
		};

		var callback = {
			success:AjaxObject.handleSuccess,
			failure:AjaxObject.handleFailure,
			scope:AjaxObject
		};

		AjaxObject.startRequest();
	}


}

function set_request_status(id, value, wwwroot){
	for(i=0;i<value.req_status.length;i++){
		if(value.req_status[i].checked==true){
			var selection = value.req_status[i].value;
		}
	}
	var url = wwwroot+'/blocks/moodlelibreserve/block_moodlelibreserve_forms.php?level=7&selection='+selection+'&sel_id='+id+'&call_num='+value.call_num.value;
	
	var AjaxObject = {
		handleSuccess:function (o) {
			document.getElementById('status'+id).innerHTML = o.responseText;
		},

		handleFailure:function (o) {
			alert(o.error+'boom');
		},

		startRequest:function () {
			YAHOO.util.Connect.asyncRequest('GET', url, callback, null);
		}
	};

	var callback = {
		success:AjaxObject.handleSuccess,
		failure:AjaxObject.handleFailure,
		scope:AjaxObject
	};

	AjaxObject.startRequest();

}

function show_hide(element){
	allDivs = document.querySelectorAll('.libres_tog');
	//allDivs = document.getElementsByClassName('libres_tog');
	for(i=0;i<allDivs.length;i++){
		allDivs[i].style.display = 'none';
	}
	myDiv = document.getElementById(element);
	myDiv.style.display='block';
}

function enter_callno(orderId){
	formId = document.getElementById('call_num'+orderId);
	formId.style.display = 'block';
}

/*function set_res_type(data){
	var AjaxObject = {
		handleSuccess:function (type) {
			var record = {type:type}
		},

		handleFailure:function (type) {
			alert('An error occurred.');
		},

		startRequest:function () {
			alert(data);
		}
	};

	var callback = {
		success:AjaxObject.handleSuccess,
		failure:AjaxObject.handleFailure,
		scope: AjaxObject
	};

	AjaxObject.startRequest();
}*/
